package com.apkplug.test.bundle;

import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import com.apkplug.test.lib.HostService;
import com.apkplug.test.lib.LoginManager;

import org.apkplug.Bundle.ApkplugOSGIService;
import org.apkplug.Bundle.OSGIServiceAgent;
import org.osgi.framework.BundleContext;

import java.util.Locale;

public class MainActivity extends Activity {

    private static final String TAG = "PluginLib";
    private TextView mTvNotes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.plugin_xxx_activity_main);
        mTvNotes = (TextView) findViewById(R.id.tv_notes);

//        startActivityForResult(new Intent(this, Main2Activity.class), 100);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d(TAG, "onActivityResult, requestCode: " + requestCode + ", resultCode: " + resultCode + ", data: " + data.getExtras().get("value"));
        setResult(resultCode, data);
        finish();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.plugin_xxx_main, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        final BundleContext bundleContext = BundleContextHolder.getInstance().getBundleContext();
        if (bundleContext == null) return false;
        final int itemId = item.getItemId();
        if (itemId == R.id.action_settings) {
            OSGIServiceAgent<LoginManager> agent = new OSGIServiceAgent<>(bundleContext, LoginManager.class, null, OSGIServiceAgent.real_time);
            try {
                agent.getService().loginAsync("haha", "hehe", new LoginManager.LoginCallback() {
                    @Override
                    public void onSuccess(String token) {
                        Log.d(TAG, "login success, token: " + token);
                        Toast.makeText(MainActivity.this, "login success, token:" + token, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFail(int error) {
                        Log.d(TAG, "login fail, error: " + error);
                        Toast.makeText(MainActivity.this, "login fail, error:" + error, Toast.LENGTH_SHORT).show();
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
            return true;
        } else if (itemId == R.id.action_get_location) {
            OSGIServiceAgent<ApkplugOSGIService> agent = new OSGIServiceAgent<>(bundleContext, ApkplugOSGIService.class, String.format(Locale.CHINA, "(serviceName=%s)", HostService.LOCATION_SERVICE), OSGIServiceAgent.real_time);
            try {
                String data = (String) agent.getService().ApkplugOSGIService(bundleContext, HostService.LOCATION_SERVICE, HostService.GET_LOCATION, null, null);
                Toast.makeText(this, data, Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return true;
        } else if (itemId == R.id.action_get_location_async) {
            OSGIServiceAgent<ApkplugOSGIService> agent = new OSGIServiceAgent<>(bundleContext, ApkplugOSGIService.class, String.format(Locale.CHINA, "(serviceName=%s)", HostService.LOCATION_SERVICE), OSGIServiceAgent.real_time);
            try {
                agent.getService().ApkplugOSGIService(bundleContext, HostService.LOCATION_SERVICE, HostService.GET_LOCATION_ASYNC, null, new HostService.Callback() {
                    @Override
                    public void onData(final String data) {
                        mTvNotes.post(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, data, Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });

            } catch (Exception e) {
                e.printStackTrace();
            }
            return true;
        } else {
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        io.github.shaobin0604.awesomelog.Log.d("onDestroy");
    }
}
